package com.lg.edu.service.impl;

import com.lg.edu.dao.LgTokenDao;
import com.lg.edu.dao.UserDao;
import com.lg.edu.pojo.LgToken;
import com.lg.edu.pojo.user.LgUser;
import com.lg.edu.service.UserService;
import com.lg.edu.service.ValidCodeService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;

import java.util.Optional;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Reference(timeout = 300000)
    private ValidCodeService validCodeService;

    @Autowired
    private UserDao userDao;

    @Autowired
    private LgTokenDao lgTokenDao;

    @Override
    public String registry(String email, String password, String code) {
        if (isRegistered(email)) {
            return "The email has been registered";
        }

        Integer validateResult = validCodeService.validate(email, code);

        if (validateResult.equals(1)) {
            return "Invalid valid code!";
        }

        if (validateResult.equals(2)) {
            return "The valid code has expired!";
        }

        // registry success, persist
        LgUser lgUser = new LgUser();
        lgUser.setEmail(email);
        lgUser.setPassword(password);
        userDao.save(lgUser);

        return "Registry success!";
    }

    @Override
    public Boolean isRegistered(String email) {
        LgUser lgUser = new LgUser();
        lgUser.setEmail(email);

        Example<LgUser> userExample = Example.of(lgUser);
        Optional<LgUser> one = userDao.findOne(userExample);

        if (one.isEmpty()) {
            return false;
        }

        return true;
    }

    @Override
    public String login(String email, String password) {
        LgUser lgUser = new LgUser();
        lgUser.setPassword(password);
        lgUser.setEmail(email);
        Example<LgUser> lgUserExample = Example.of(lgUser);
        Optional<LgUser> one = userDao.findOne(lgUserExample);

        if (one.isEmpty()) {
            return null;
        }

        // generate one token [UUID]
        String uuid = UUID.randomUUID().toString();
        // persist token
        LgToken lgToken = new LgToken();
        lgToken.setEmail(email);
        lgToken.setToken(uuid);
        lgTokenDao.save(lgToken);

        return uuid;
    }

    @Override
    public String getEmailByToken(String token) {
        LgToken lgToken = new LgToken();
        lgToken.setToken(token);

        Example<LgToken> lgTokenExample = Example.of(lgToken);
        Optional<LgToken> one = lgTokenDao.findOne(lgTokenExample);

        if (one.isEmpty()) {
            return "";
        }

        return one.get().getEmail();
    }

    @Override
    public String getValidCodeByEmail(String email) {
        Boolean isSended = validCodeService.create(email);
        if (isSended) {
            return "The verification code has been sent to your email, please check!";
        } else {
            return "Verification code sending failed! Registration is temporarily unavailable";
        }
    }
}
